package net.keebook.webapp.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.persistence.Query;

import net.keebook.webapp.entity.Setting;
import net.keebook.webapp.util.SettingId;


@Stateless
public class SettingDao extends Dao<Setting> {
	@Override
	public void store(Setting item) {
		super.store(item);
	}
	
	@Override
	public void update(Setting item) {
		super.update(item);
	}
	
	@Override
	public Setting get(Long id) {
		throw new IllegalAccessError();
	}
	
	@Override
	public Setting get(String id) {
		throw new IllegalAccessError();
	}
	
	public Setting get(SettingId id) {
		Setting item = getEntityManager().find(Setting.class, id);
		getEntityManager().detach(item);
		return item;
	}
	
	@Override
	public void delete(Setting item) {
		super.delete(item);
	}
	
	@SuppressWarnings("unchecked")
	public List<Setting> getList() {
		Query query = getEntityManager().createQuery("FROM Setting s");
		return detachAll(query.getResultList());
	}
	
	public Map<SettingId, Setting> getMap() {
		List<Setting> settings = getList();
		HashMap<SettingId, Setting> result = new HashMap<SettingId, Setting>();
		for (Setting s : settings) {
			getEntityManager().detach(s);
			result.put(s.getId(), s);
		}
		return result;
	}
}
